package com.lucy.property.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.property.entity.PropertyBuilding;
import com.lucy.property.module.fee.FeeItem;
import com.lucy.property.module.fee.FeeItemService;
import com.lucy.property.module.fee.params.FeeItemCreateParams;
import com.lucy.property.module.fee.params.FeeItemQueryParams;
import com.lucy.property.module.fee.params.FeeItemUpdateParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author kindear
 */
@RestController
@Api(tags = "费用管理")
public class FeeItemController {
    @Autowired
    FeeItemService service;

    @PostMapping("fee")
    @ApiOperation(value = "费用新建",notes = "")
    public ResponseDTO<Object> feeCreate(@RequestBody @Validated FeeItemCreateParams params){
        FeeItem item = new FeeItem();
        BeanUtil.copyProperties(params,item, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(
                service.saveOne(item)
        );
    }

    @DeleteMapping("fee/{id}")
    @ApiOperation(value = "费用删除",notes = "")
    public ResponseDTO<Object> feeDelete(@PathVariable("id") String id){
        service.deleteOne(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @PutMapping("fee")
    @ApiOperation(value = "费用更新",notes = "")
    public ResponseDTO<Object> feeUpdate(@RequestBody @Validated FeeItemUpdateParams params){
        Optional<FeeItem> itemOp = service.getOne(params.getId());
        if (!itemOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        FeeItem item = itemOp.get();
        BeanUtil.copyProperties(params,item,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(
                service.updateOne(item)
        );
    }

    @GetMapping("fee")
    @ApiOperation(value = "费用列表",notes = "")
    public ResponseDTO<Object> feeListQuery(FeeItemQueryParams params){
        Sort sort;
        if("DESC".equalsIgnoreCase(params.getOrder())){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        FeeItem item = new FeeItem();
        BeanUtil.copyProperties(params,item,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<FeeItem> example = Example.of(item);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("fee/{id}")
    @ApiOperation(value = "费用查询",notes = "")
    public ResponseDTO<Object> feeQuery(@PathVariable("id") String id){
        Optional<FeeItem> itemOp = service.getOne(id);
        if (!itemOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        return ResponseDTO.succData(
                itemOp.get()
        );
    }

}
